home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zcontrol.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  23.5 KB  |  956 lines

  1. /* Copyright (C) 1989, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zcontrol.c,v 1.3 2000/09/19 19:00:53 lpd Exp $ */
  20. /* Control operators */
  21. #include "string_.h"
  22. #include "ghost.h"
  23. #include "stream.h"
  24. #include "oper.h"
  25. #include "estack.h"
  26. #include "files.h"
  27. #include "ipacked.h"
  28. #include "iutil.h"
  29. #include "store.h"
  30.  
  31. /* Forward references */
  32. private int no_cleanup(P1(i_ctx_t *));
  33. private uint count_exec_stack(P2(i_ctx_t *, bool));
  34. private uint count_to_stopped(P2(i_ctx_t *, long));
  35. private int unmatched_exit(P2(os_ptr, op_proc_t));
  36.  
  37. /* See the comment in opdef.h for an invariant which allows */
  38. /* more efficient implementation of for, loop, and repeat. */
  39.  
  40. /* <[test0 body0 ...]> .cond - */
  41. private int cond_continue(P1(i_ctx_t *));
  42. private int
  43. zcond(i_ctx_t *i_ctx_p)
  44. {
  45.     os_ptr op = osp;
  46.     es_ptr ep = esp;
  47.  
  48.     /* Push the array on the e-stack and call the continuation. */
  49.     if (!r_is_array(op))
  50.     return_op_typecheck(op);
  51.     check_execute(*op);
  52.     if ((r_size(op) & 1) != 0)
  53.     return_error(e_rangecheck);
  54.     if (r_size(op) == 0)
  55.     return zpop(i_ctx_p);
  56.     check_estack(3);
  57.     esp = ep += 3;
  58.     ref_assign(ep - 2, op);    /* the cond body */
  59.     make_op_estack(ep - 1, cond_continue);
  60.     array_get(op, 0L, ep);
  61.     esfile_check_cache();
  62.     pop(1);
  63.     return o_push_estack;
  64. }
  65. private int
  66. cond_continue(i_ctx_t *i_ctx_p)
  67. {
  68.     os_ptr op = osp;
  69.     es_ptr ep = esp;
  70.     int code;
  71.  
  72.     /* The top element of the e-stack is the remaining tail of */
  73.     /* the cond body.  The top element of the o-stack should be */
  74.     /* the (boolean) result of the test that is the first element */
  75.     /* of the tail. */
  76.     check_type(*op, t_boolean);
  77.     if (op->value.boolval) {    /* true */
  78.     array_get(ep, 1L, ep);
  79.     esfile_check_cache();
  80.     code = o_pop_estack;
  81.     } else if (r_size(ep) > 2) {    /* false */
  82.     const ref_packed *elts = ep->value.packed;
  83.  
  84.     check_estack(2);
  85.     r_dec_size(ep, 2);
  86.     elts = packed_next(elts);
  87.     elts = packed_next(elts);
  88.     ep->value.packed = elts;
  89.     array_get(ep, 0L, ep + 2);
  90.     make_op_estack(ep + 1, cond_continue);
  91.     esp = ep + 2;
  92.     esfile_check_cache();
  93.     code = o_push_estack;
  94.     } else {            /* fall off end of cond */
  95.     esp = ep - 1;
  96.     code = o_pop_estack;
  97.     }
  98.     pop(1);            /* get rid of the boolean */
  99.     return code;
  100. }
  101.  
  102. /* <obj> exec - */
  103. int
  104. zexec(i_ctx_t *i_ctx_p)
  105. {
  106.     os_ptr op = osp;
  107.  
  108.     check_op(1);
  109.     if (!r_has_attr(op, a_executable))
  110.     return 0;        /* literal object just gets pushed back */
  111.     check_estack(1);
  112.     ++esp;
  113.     ref_assign(esp, op);
  114.     esfile_check_cache();
  115.     pop(1);
  116.     return o_push_estack;
  117. }
  118.  
  119. /* <obj1> ... <objn> <n> .execn - */
  120. private int
  121. zexecn(i_ctx_t *i_ctx_p)
  122. {
  123.     os_ptr op = osp;
  124.     uint n, i;
  125.     es_ptr esp_orig;
  126.  
  127.     check_int_leu(*op, max_uint - 1);
  128.     n = (uint) op->value.intval;
  129.     check_op(n + 1);
  130.     check_estack(n);
  131.     esp_orig = esp;
  132.     for (i = 0; i < n; ++i) {
  133.     const ref *rp = ref_stack_index(&o_stack, (long)(i + 1));
  134.  
  135.     /* Make sure this object is legal to execute. */
  136.     if (ref_type_uses_access(r_type(rp))) {
  137.         if (!r_has_attr(rp, a_execute) &&
  138.         r_has_attr(rp, a_executable)
  139.         ) {
  140.         esp = esp_orig;
  141.         return_error(e_invalidaccess);
  142.         }
  143.     }
  144.     /* Executable nulls have a special meaning on the e-stack, */
  145.     /* so since they are no-ops, don't push them. */
  146.     if (!r_has_type_attrs(rp, t_null, a_executable)) {
  147.         ++esp;
  148.         ref_assign(esp, rp);
  149.     }
  150.     }
  151.     esfile_check_cache();
  152.     pop(n + 1);
  153.     return o_push_estack;
  154. }
  155.  
  156. /* <obj> superexec - */
  157. private int end_superexec(P1(i_ctx_t *));
  158. private int
  159. zsuperexec(i_ctx_t *i_ctx_p)
  160. {
  161.     os_ptr op = osp;
  162.     es_ptr ep;
  163.  
  164.     check_op(1);
  165.     if (!r_has_attr(op, a_executable))
  166.     return 0;        /* literal object just gets pushed back */
  167.     check_estack(2);
  168.     ep = esp += 3;
  169.     make_mark_estack(ep - 2, es_other, end_superexec); /* error case */
  170.     make_op_estack(ep - 1,  end_superexec); /* normal case */
  171.     ref_assign(ep, op);
  172.     esfile_check_cache();
  173.     pop(1);
  174.     i_ctx_p->in_superexec++;
  175.     return o_push_estack;
  176. }
  177. private int
  178. end_superexec(i_ctx_t *i_ctx_p)
  179. {
  180.     i_ctx_p->in_superexec--;
  181.     return 0;
  182. }
  183.  
  184. /* <bool> <proc> if - */
  185. int
  186. zif(i_ctx_t *i_ctx_p)
  187. {
  188.     os_ptr op = osp;
  189.  
  190.     check_type(op[-1], t_boolean);
  191.     check_proc(*op);
  192.     if (op[-1].value.boolval) {
  193.     check_estack(1);
  194.     ++esp;
  195.     ref_assign(esp, op);
  196.     esfile_check_cache();
  197.     }
  198.     pop(2);
  199.     return o_push_estack;
  200. }
  201.  
  202. /* <bool> <proc_true> <proc_false> ifelse - */
  203. int
  204. zifelse(i_ctx_t *i_ctx_p)
  205. {
  206.     os_ptr op = osp;
  207.  
  208.     check_type(op[-2], t_boolean);
  209.     check_proc(op[-1]);
  210.     check_proc(*op);
  211.     check_estack(1);
  212.     ++esp;
  213.     if (op[-2].value.boolval) {
  214.     ref_assign(esp, op - 1);
  215.     } else {
  216.     ref_assign(esp, op);
  217.     }
  218.     esfile_check_cache();
  219.     pop(3);
  220.     return o_push_estack;
  221. }
  222.  
  223. /* <init> <step> <limit> <proc> for - */
  224. private int
  225.     for_pos_int_continue(P1(i_ctx_t *)),
  226.     for_neg_int_continue(P1(i_ctx_t *)),
  227.     for_real_continue(P1(i_ctx_t *));
  228. int
  229. zfor(i_ctx_t *i_ctx_p)
  230. {
  231.     os_ptr op = osp;
  232.     register es_ptr ep;
  233.  
  234.     check_estack(7);
  235.     ep = esp + 6;
  236.     check_proc(*op);
  237.     /* Push a mark, the control variable, the initial value, */
  238.     /* the increment, the limit, and the procedure, */
  239.     /* and invoke the continuation operator. */
  240.     if (r_has_type(op - 3, t_integer) &&
  241.     r_has_type(op - 2, t_integer)
  242.     ) {
  243.     make_int(ep - 4, op[-3].value.intval);
  244.     make_int(ep - 3, op[-2].value.intval);
  245.     switch (r_type(op - 1)) {
  246.         case t_integer:
  247.         make_int(ep - 2, op[-1].value.intval);
  248.         break;
  249.         case t_real:
  250.         make_int(ep - 2, (long)op[-1].value.realval);
  251.         break;
  252.         default:
  253.         return_op_typecheck(op - 1);
  254.     }
  255.     if (ep[-3].value.intval >= 0)
  256.         make_op_estack(ep, for_pos_int_continue);
  257.     else
  258.         make_op_estack(ep, for_neg_int_continue);
  259.     } else {
  260.     float params[3];
  261.     int code;
  262.  
  263.     if ((code = float_params(op - 1, 3, params)) < 0)
  264.         return code;
  265.     make_real(ep - 4, params[0]);
  266.     make_real(ep - 3, params[1]);
  267.     make_real(ep - 2, params[2]);
  268.     make_op_estack(ep, for_real_continue);
  269.     }
  270.     make_mark_estack(ep - 5, es_for, no_cleanup);
  271.     ref_assign(ep - 1, op);
  272.     esp = ep;
  273.     pop(4);
  274.     return o_push_estack;
  275. }
  276. /* Continuation operators for for, separate for positive integer, */
  277. /* negative integer, and real. */
  278. /* Execution stack contains mark, control variable, increment, */
  279. /* limit, and procedure (procedure is topmost.) */
  280. /* Continuation operator for positive integers. */
  281. private int
  282. for_pos_int_continue(i_ctx_t *i_ctx_p)
  283. {
  284.     os_ptr op = osp;
  285.     register es_ptr ep = esp;
  286.     long var = ep[-3].value.intval;
  287.  
  288.     if (var > ep[-1].value.intval) {
  289.     esp -= 5;        /* pop everything */
  290.     return o_pop_estack;
  291.     }
  292.     push(1);
  293.     make_int(op, var);
  294.     ep[-3].value.intval = var + ep[-2].value.intval;
  295.     ref_assign_inline(ep + 2, ep);    /* saved proc */
  296.     esp = ep + 2;
  297.     return o_push_estack;
  298. }
  299. /* Continuation operator for negative integers. */
  300. private int
  301. for_neg_int_continue(i_ctx_t *i_ctx_p)
  302. {
  303.     os_ptr op = osp;
  304.     register es_ptr ep = esp;
  305.     long var = ep[-3].value.intval;
  306.  
  307.     if (var < ep[-1].value.intval) {
  308.     esp -= 5;        /* pop everything */
  309.     return o_pop_estack;
  310.     }
  311.     push(1);
  312.     make_int(op, var);
  313.     ep[-3].value.intval = var + ep[-2].value.intval;
  314.     ref_assign(ep + 2, ep);    /* saved proc */
  315.     esp = ep + 2;
  316.     return o_push_estack;
  317. }
  318. /* Continuation operator for reals. */
  319. private int
  320. for_real_continue(i_ctx_t *i_ctx_p)
  321. {
  322.     os_ptr op = osp;
  323.     es_ptr ep = esp;
  324.     float var = ep[-3].value.realval;
  325.     float incr = ep[-2].value.realval;
  326.  
  327.     if (incr >= 0 ? (var > ep[-1].value.realval) :
  328.     (var < ep[-1].value.realval)
  329.     ) {
  330.     esp -= 5;        /* pop everything */
  331.     return o_pop_estack;
  332.     }
  333.     push(1);
  334.     ref_assign(op, ep - 3);
  335.     ep[-3].value.realval = var + incr;
  336.     esp = ep + 2;
  337.     ref_assign(ep + 2, ep);    /* saved proc */
  338.     return o_push_estack;
  339. }
  340.  
  341. /* Here we provide an internal variant of 'for' that enumerates the */
  342. /* values 0, 1/N, 2/N, ..., 1 precisely.  The arguments must be */
  343. /* the integers 0, 1, and N.  We need this for */
  344. /* loading caches such as the transfer function cache. */
  345. private int for_fraction_continue(P1(i_ctx_t *));
  346. int
  347. zfor_fraction(i_ctx_t *i_ctx_p)
  348. {
  349.     int code = zfor(i_ctx_p);
  350.  
  351.     if (code < 0)
  352.     return code;        /* shouldn't ever happen! */
  353.     make_op_estack(esp, for_fraction_continue);
  354.     return code;
  355. }
  356. /* Continuation procedure */
  357. private int
  358. for_fraction_continue(i_ctx_t *i_ctx_p)
  359. {
  360.     register es_ptr ep = esp;
  361.     int code = for_pos_int_continue(i_ctx_p);
  362.  
  363.     if (code != o_push_estack)
  364.     return code;
  365.     /* We must use osp instead of op here, because */
  366.     /* for_pos_int_continue pushes a value on the o-stack. */
  367.     make_real(osp, (float)osp->value.intval / ep[-1].value.intval);
  368.     return code;
  369. }
  370.  
  371. /* <int> <proc> repeat - */
  372. private int repeat_continue(P1(i_ctx_t *));
  373. private int
  374. zrepeat(i_ctx_t *i_ctx_p)
  375. {
  376.     os_ptr op = osp;
  377.     check_type(op[-1], t_integer);
  378.     check_proc(*op);
  379.     if (op[-1].value.intval < 0)
  380.     return_error(e_rangecheck);
  381.     check_estack(5);
  382.     /* Push a mark, the count, and the procedure, and invoke */
  383.     /* the continuation operator. */
  384.     push_mark_estack(es_for, no_cleanup);
  385.     *++esp = op[-1];
  386.     *++esp = *op;
  387.     make_op_estack(esp + 1, repeat_continue);
  388.     pop(2);
  389.     return repeat_continue(i_ctx_p);
  390. }
  391. /* Continuation operator for repeat */
  392. private int
  393. repeat_continue(i_ctx_t *i_ctx_p)
  394. {
  395.     es_ptr ep = esp;        /* saved proc */
  396.  
  397.     if (--(ep[-1].value.intval) >= 0) {        /* continue */
  398.     esp += 2;
  399.     ref_assign(esp, ep);
  400.     return o_push_estack;
  401.     } else {            /* done */
  402.     esp -= 3;        /* pop mark, count, proc */
  403.     return o_pop_estack;
  404.     }
  405. }
  406.  
  407. /* <proc> loop */
  408. private int loop_continue(P1(i_ctx_t *));
  409. private int
  410. zloop(i_ctx_t *i_ctx_p)
  411. {
  412.     os_ptr op = osp;
  413.  
  414.     check_proc(*op);
  415.     check_estack(4);
  416.     /* Push a mark and the procedure, and invoke */
  417.     /* the continuation operator. */
  418.     push_mark_estack(es_for, no_cleanup);
  419.     *++esp = *op;
  420.     make_op_estack(esp + 1, loop_continue);
  421.     pop(1);
  422.     return loop_continue(i_ctx_p);
  423. }
  424. /* Continuation operator for loop */
  425. private int
  426. loop_continue(i_ctx_t *i_ctx_p)
  427. {
  428.     register es_ptr ep = esp;    /* saved proc */
  429.  
  430.     ref_assign(ep + 2, ep);
  431.     esp = ep + 2;
  432.     return o_push_estack;
  433. }
  434.  
  435. /* - exit - */
  436. private int
  437. zexit(i_ctx_t *i_ctx_p)
  438. {
  439.     os_ptr op = osp;
  440.     ref_stack_enum_t rsenum;
  441.     uint scanned = 0;
  442.  
  443.     ref_stack_enum_begin(&rsenum, &e_stack);
  444.     do {
  445.     uint used = rsenum.size;
  446.     es_ptr ep = rsenum.ptr + used - 1;
  447.     uint count = used;
  448.  
  449.     for (; count; count--, ep--)
  450.         if (r_is_estack_mark(ep))
  451.         switch (estack_mark_index(ep)) {
  452.             case es_for:
  453.             pop_estack(i_ctx_p, scanned + (used - count + 1));
  454.             return o_pop_estack;
  455.             case es_stopped:
  456.             return_error(e_invalidexit);    /* not a loop */
  457.         }
  458.     scanned += used;
  459.     } while (ref_stack_enum_next(&rsenum));
  460.     /* No mark, quit.  (per Adobe documentation) */
  461.     push(2);
  462.     return unmatched_exit(op, zexit);
  463. }
  464.  
  465. /*
  466.  * .stopped pushes the following on the e-stack:
  467.  *      - A mark with type = es_stopped and procedure = no_cleanup.
  468.  *      - The result to be pushed on a normal return.
  469.  *      - The signal mask for .stop.
  470.  *      - The procedure %stopped_push, to handle the normal return case.
  471.  */
  472.  
  473. /* In the normal (no-error) case, pop the mask from the e-stack, */
  474. /* and move the result to the o-stack. */
  475. private int
  476. stopped_push(i_ctx_t *i_ctx_p)
  477. {
  478.     os_ptr op = osp;
  479.  
  480.     push(1);
  481.     *op = esp[-1];
  482.     esp -= 3;
  483.     return o_pop_estack;
  484. }
  485.  
  486. /* - stop - */
  487. /* Equivalent to true 1 .stop. */
  488. /* This is implemented in C because if were a pseudo-operator, */
  489. /* the stacks would get restored in case of an error. */
  490. private int
  491. zstop(i_ctx_t *i_ctx_p)
  492. {
  493.     os_ptr op = osp;
  494.     uint count = count_to_stopped(i_ctx_p, 1L);
  495.  
  496.     if (count) {
  497.     /*
  498.      * If there are any t_oparrays on the e-stack, they will pop
  499.      * any new items from the o-stack.  Wait to push the 'true'
  500.      * until we have run all the unwind procedures.
  501.      */
  502.     check_ostack(2);
  503.     pop_estack(i_ctx_p, count);
  504.     op = osp;
  505.     push(1);
  506.     make_true(op);
  507.     return o_pop_estack;
  508.     }
  509.     /* No mark, quit.  (per Adobe documentation) */
  510.     push(2);
  511.     return unmatched_exit(op, zstop);
  512. }
  513.  
  514. /* <result> <mask> .stop - */
  515. private int
  516. zzstop(i_ctx_t *i_ctx_p)
  517. {
  518.     os_ptr op = osp;
  519.     uint count;
  520.  
  521.     check_type(*op, t_integer);
  522.     count = count_to_stopped(i_ctx_p, op->value.intval);
  523.     if (count) {
  524.     /*
  525.      * If there are any t_oparrays on the e-stack, they will pop
  526.      * any new items from the o-stack.  Wait to push the result
  527.      * until we have run all the unwind procedures.
  528.      */
  529.     ref save_result;
  530.  
  531.     check_op(2);
  532.     save_result = op[-1];
  533.     pop(2);
  534.     pop_estack(i_ctx_p, count);
  535.     op = osp;
  536.     push(1);
  537.     *op = save_result;
  538.     return o_pop_estack;
  539.     }
  540.     /* No mark, quit.  (per Adobe documentation) */
  541.     return unmatched_exit(op, zzstop);
  542. }
  543.  
  544. /* <obj> stopped <stopped> */
  545. /* Equivalent to false 1 .stopped. */
  546. /* This is implemented in C because if were a pseudo-operator, */
  547. /* the stacks would get restored in case of an error. */
  548. private int
  549. zstopped(i_ctx_t *i_ctx_p)
  550. {
  551.     os_ptr op = osp;
  552.     check_op(1);
  553.     /* Mark the execution stack, and push the default result */
  554.     /* in case control returns normally. */
  555.     check_estack(5);
  556.     push_mark_estack(es_stopped, no_cleanup);
  557.     ++esp;
  558.     make_false(esp);        /* save the result */
  559.     ++esp;
  560.     make_int(esp, 1);        /* save the signal mask */
  561.     push_op_estack(stopped_push);
  562.     *++esp = *op;        /* execute the operand */
  563.     esfile_check_cache();
  564.     pop(1);
  565.     return o_push_estack;
  566. }
  567.  
  568. /* <obj> <result> <mask> .stopped <result> */
  569. private int
  570. zzstopped(i_ctx_t *i_ctx_p)
  571. {
  572.     os_ptr op = osp;
  573.     check_type(*op, t_integer);
  574.     check_op(3);
  575.     /* Mark the execution stack, and push the default result */
  576.     /* in case control returns normally. */
  577.     check_estack(5);
  578.     push_mark_estack(es_stopped, no_cleanup);
  579.     *++esp = op[-1];        /* save the result */
  580.     *++esp = *op;        /* save the signal mask */
  581.     push_op_estack(stopped_push);
  582.     *++esp = op[-2];        /* execute the operand */
  583.     esfile_check_cache();
  584.     pop(3);
  585.     return o_push_estack;
  586. }
  587.  
  588. /* <mask> .instopped false */
  589. /* <mask> .instopped <result> true */
  590. private int
  591. zinstopped(i_ctx_t *i_ctx_p)
  592. {
  593.     os_ptr op = osp;
  594.     uint count;
  595.  
  596.     check_type(*op, t_integer);
  597.     count = count_to_stopped(i_ctx_p, op->value.intval);
  598.     if (count) {
  599.     push(1);
  600.     op[-1] = *ref_stack_index(&e_stack, count - 2);        /* default result */
  601.     make_true(op);
  602.     } else
  603.     make_false(op);
  604.     return 0;
  605. }
  606.  
  607. /* <include_marks> .countexecstack <int> */
  608. /* - countexecstack <int> */
  609. /* countexecstack is an operator solely for the sake of the Genoa tests. */
  610. private int
  611. zcountexecstack(i_ctx_t *i_ctx_p)
  612. {
  613.     os_ptr op = osp;
  614.  
  615.     push(1);
  616.     make_int(op, count_exec_stack(i_ctx_p, false));
  617.     return 0;
  618. }
  619. private int
  620. zcountexecstack1(i_ctx_t *i_ctx_p)
  621. {
  622.     os_ptr op = osp;
  623.  
  624.     check_type(*op, t_boolean);
  625.     make_int(op, count_exec_stack(i_ctx_p, op->value.boolval));
  626.     return 0;
  627. }
  628.  
  629. /* <array> <include_marks> .execstack <subarray> */
  630. /* <array> execstack <subarray> */
  631. /* execstack is an operator solely for the sake of the Genoa tests. */
  632. private int execstack_continue(P1(i_ctx_t *));
  633. private int execstack2_continue(P1(i_ctx_t *));
  634. private int
  635. push_execstack(i_ctx_t *i_ctx_p, os_ptr op1, bool include_marks,
  636.            op_proc_t cont)
  637. {
  638.     uint size;
  639.     /*
  640.      * We can't do this directly, because the interpreter
  641.      * might have cached some state.  To force the interpreter
  642.      * to update the stored state, we push a continuation on
  643.      * the exec stack; the continuation is executed immediately,
  644.      * and does the actual transfer.
  645.      */
  646.     uint depth;
  647.  
  648.     check_write_type(*op1, t_array);
  649.     size = r_size(op1);
  650.     depth = count_exec_stack(i_ctx_p, include_marks);
  651.     if (depth > size)
  652.     return_error(e_rangecheck);
  653.     {
  654.     int code = ref_stack_store_check(&e_stack, op1, size, 0);
  655.  
  656.     if (code < 0)
  657.         return code;
  658.     }
  659.     check_estack(1);
  660.     r_set_size(op1, depth);
  661.     push_op_estack(cont);
  662.     return o_push_estack;
  663. }
  664. private int
  665. zexecstack(i_ctx_t *i_ctx_p)
  666. {
  667.     os_ptr op = osp;
  668.  
  669.     return push_execstack(i_ctx_p, op, false, execstack_continue);
  670. }
  671. private int
  672. zexecstack2(i_ctx_t *i_ctx_p)
  673. {
  674.     os_ptr op = osp;
  675.  
  676.     check_type(*op, t_boolean);
  677.     return push_execstack(i_ctx_p, op - 1, op->value.boolval, execstack2_continue);
  678. }
  679. /* Continuation operator to do the actual transfer. */
  680. /* r_size(op1) was set just above. */
  681. private int
  682. do_execstack(i_ctx_t *i_ctx_p, bool include_marks, os_ptr op1)
  683. {
  684.     os_ptr op = osp;
  685.     ref *arefs = op1->value.refs;
  686.     uint asize = r_size(op1);
  687.     uint i;
  688.     ref *rq;
  689.  
  690.     /*
  691.      * Copy elements from the stack to the array,
  692.      * optionally skipping executable nulls.
  693.      * Clear the executable bit in any internal operators, and
  694.      * convert t_structs and t_astructs (which can only appear
  695.      * in connection with stack marks, which means that they will
  696.      * probably be freed when unwinding) to something harmless.
  697.      */
  698.     for (i = 0, rq = arefs + asize; rq != arefs; ++i) {
  699.     const ref *rp = ref_stack_index(&e_stack, (long)i);
  700.  
  701.     if (r_has_type_attrs(rp, t_null, a_executable) && !include_marks)
  702.         continue;
  703.     --rq;
  704.     ref_assign_old(op1, rq, rp, "execstack");
  705.     switch (r_type(rq)) {
  706.         case t_operator: {
  707.         uint opidx = op_index(rq);
  708.  
  709.         if (opidx == 0 || op_def_is_internal(op_index_def(opidx)))
  710.             r_clear_attrs(rq, a_executable);
  711.         break;
  712.         }
  713.         case t_struct:
  714.         case t_astruct: {
  715.         const char *tname =
  716.             gs_struct_type_name_string(
  717.                 gs_object_type(imemory, rq->value.pstruct));
  718.  
  719.         make_const_string(rq, a_readonly | avm_foreign,
  720.                   strlen(tname), (const byte *)tname);
  721.         break;
  722.         }
  723.         default:
  724.         ;
  725.     }
  726.     }
  727.     pop(op - op1);
  728.     return 0;
  729. }
  730. private int
  731. execstack_continue(i_ctx_t *i_ctx_p)
  732. {
  733.     os_ptr op = osp;
  734.  
  735.     return do_execstack(i_ctx_p, false, op);
  736. }
  737. private int
  738. execstack2_continue(i_ctx_t *i_ctx_p)
  739. {
  740.     os_ptr op = osp;
  741.  
  742.     return do_execstack(i_ctx_p, op->value.boolval, op - 1);
  743. }
  744.  
  745. /* - .needinput - */
  746. private int
  747. zneedinput(i_ctx_t *i_ctx_p)
  748. {
  749.     return e_NeedInput;        /* interpreter will exit to caller */
  750. }
  751.  
  752. /* <obj> <int> .quit - */
  753. private int
  754. zquit(i_ctx_t *i_ctx_p)
  755. {
  756.     os_ptr op = osp;
  757.  
  758.     check_op(2);
  759.     check_type(*op, t_integer);
  760.     return_error(e_Quit);    /* Interpreter will do the exit */
  761. }
  762.  
  763. /* - currentfile <file> */
  764. private ref *zget_current_file(P1(i_ctx_t *));
  765. private int
  766. zcurrentfile(i_ctx_t *i_ctx_p)
  767. {
  768.     os_ptr op = osp;
  769.     ref *fp;
  770.  
  771.     push(1);
  772.     /* Check the cache first */
  773.     if (esfile != 0) {
  774. #ifdef DEBUG
  775.     /* Check that esfile is valid. */
  776.     ref *efp = zget_current_file(i_ctx_p);
  777.  
  778.     if (esfile != efp) {
  779.         lprintf2("currentfile: esfile=0x%lx, efp=0x%lx\n",
  780.              (ulong) esfile, (ulong) efp);
  781.         ref_assign(op, efp);
  782.     } else
  783. #endif
  784.         ref_assign(op, esfile);
  785.     } else if ((fp = zget_current_file(i_ctx_p)) == 0) {    /* Return an invalid file object. */
  786.     /* This doesn't make a lot of sense to me, */
  787.     /* but it's what the PostScript manual specifies. */
  788.     make_invalid_file(op);
  789.     } else {
  790.     ref_assign(op, fp);
  791.     esfile_set_cache(fp);
  792.     }
  793.     /* Make the returned value literal. */
  794.     r_clear_attrs(op, a_executable);
  795.     return 0;
  796. }
  797. /* Get the current file from which the interpreter is reading. */
  798. private ref *
  799. zget_current_file(i_ctx_t *i_ctx_p)
  800. {
  801.     ref_stack_enum_t rsenum;
  802.  
  803.     ref_stack_enum_begin(&rsenum, &e_stack);
  804.     do {
  805.     uint count = rsenum.size;
  806.     es_ptr ep = rsenum.ptr + count - 1;
  807.  
  808.     for (; count; count--, ep--)
  809.         if (r_has_type_attrs(ep, t_file, a_executable))
  810.         return ep;
  811.     } while (ref_stack_enum_next(&rsenum));
  812.     return 0;
  813. }
  814.  
  815. /* ------ Initialization procedure ------ */
  816.  
  817. /* We need to split the table because of the 16-element limit. */
  818. const op_def zcontrol1_op_defs[] = {
  819.     {"1.cond", zcond},
  820.     {"0countexecstack", zcountexecstack},
  821.     {"1.countexecstack", zcountexecstack1},
  822.     {"0currentfile", zcurrentfile},
  823.     {"1exec", zexec},
  824.     {"1.execn", zexecn},
  825.     {"1execstack", zexecstack},
  826.     {"2.execstack", zexecstack2},
  827.     {"0exit", zexit},
  828.     {"2if", zif},
  829.     {"3ifelse", zifelse},
  830.     {"0.instopped", zinstopped},
  831.     {"0.needinput", zneedinput},
  832.     op_def_end(0)
  833. };
  834. const op_def zcontrol2_op_defs[] = {
  835.     {"4for", zfor},
  836.     {"1loop", zloop},
  837.     {"2.quit", zquit},
  838.     {"2repeat", zrepeat},
  839.     {"0stop", zstop},
  840.     {"1.stop", zzstop},
  841.     {"1stopped", zstopped},
  842.     {"2.stopped", zzstopped},
  843.     op_def_end(0)
  844. };
  845. const op_def zcontrol3_op_defs[] = {
  846.         /* Internal operators */
  847.     {"1%cond_continue", cond_continue},
  848.     {"1%execstack_continue", execstack_continue},
  849.     {"2%execstack2_continue", execstack2_continue},
  850.     {"0%for_pos_int_continue", for_pos_int_continue},
  851.     {"0%for_neg_int_continue", for_neg_int_continue},
  852.     {"0%for_real_continue", for_real_continue},
  853.     {"4%for_fraction", zfor_fraction},
  854.     {"0%for_fraction_continue", for_fraction_continue},
  855.     {"0%loop_continue", loop_continue},
  856.     {"0%repeat_continue", repeat_continue},
  857.     {"0%stopped_push", stopped_push},
  858.     {"1superexec", zsuperexec},
  859.     {"0%end_superexec", end_superexec},
  860.     op_def_end(0)
  861. };
  862.  
  863. /* ------ Internal routines ------ */
  864.  
  865. /* Vacuous cleanup routine */
  866. private int
  867. no_cleanup(i_ctx_t *i_ctx_p)
  868. {
  869.     return 0;
  870. }
  871.  
  872. /*
  873.  * Count the number of elements on the exec stack, with or without
  874.  * the normally invisible elements (*op is a Boolean that indicates this).
  875.  */
  876. private uint
  877. count_exec_stack(i_ctx_t *i_ctx_p, bool include_marks)
  878. {
  879.     uint count = ref_stack_count(&e_stack);
  880.  
  881.     if (!include_marks) {
  882.     uint i;
  883.  
  884.     for (i = count; i--;)
  885.         if (r_has_type_attrs(ref_stack_index(&e_stack, (long)i),
  886.                  t_null, a_executable))
  887.         --count;
  888.     }
  889.     return count;
  890. }
  891.  
  892. /*
  893.  * Count the number of elements down to and including the first 'stopped'
  894.  * mark on the e-stack with a given mask.  Return 0 if there is no 'stopped'
  895.  * mark.
  896.  */
  897. private uint
  898. count_to_stopped(i_ctx_t *i_ctx_p, long mask)
  899. {
  900.     ref_stack_enum_t rsenum;
  901.     uint scanned = 0;
  902.  
  903.     ref_stack_enum_begin(&rsenum, &e_stack);
  904.     do {
  905.     uint used = rsenum.size;
  906.     es_ptr ep = rsenum.ptr + used - 1;
  907.     uint count = used;
  908.  
  909.     for (; count; count--, ep--)
  910.         if (r_is_estack_mark(ep) &&
  911.         estack_mark_index(ep) == es_stopped &&
  912.         (ep[2].value.intval & mask) != 0
  913.         )
  914.         return scanned + (used - count + 1);
  915.     scanned += used;
  916.     } while (ref_stack_enum_next(&rsenum));
  917.     return 0;
  918. }
  919.  
  920. /*
  921.  * Pop the e-stack, executing cleanup procedures as needed.
  922.  * We could make this more efficient using ref_stack_enum_*,
  923.  * but it isn't used enough to make this worthwhile.
  924.  */
  925. void
  926. pop_estack(i_ctx_t *i_ctx_p, uint count)
  927. {
  928.     uint idx = 0;
  929.     uint popped = 0;
  930.  
  931.     esfile_clear_cache();
  932.     for (; idx < count; idx++) {
  933.     ref *ep = ref_stack_index(&e_stack, idx - popped);
  934.  
  935.     if (r_is_estack_mark(ep)) {
  936.         ref_stack_pop(&e_stack, idx + 1 - popped);
  937.         popped = idx + 1;
  938.         (*real_opproc(ep)) (i_ctx_p);
  939.     }
  940.     }
  941.     ref_stack_pop(&e_stack, count - popped);
  942. }
  943.  
  944. /*
  945.  * Execute a quit in the case of an exit or stop with no appropriate
  946.  * enclosing control scope (loop or stopped).  The caller has already
  947.  * ensured two free slots on the top of the o-stack.
  948.  */
  949. private int
  950. unmatched_exit(os_ptr op, op_proc_t opproc)
  951. {
  952.     make_oper(op - 1, 0, opproc);
  953.     make_int(op, e_invalidexit);
  954.     return_error(e_Quit);
  955. }
  956.